/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmppbot.parse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;
import xmppbot.Main;
import xmppbot.db;

/**
 *
 * @author lian
 */
public class options {
    public  db             MySQLDB         = Main.Connect.mysqldb;        
    private Date           date            = null;
    private String         MsgType         = null; // Type of message:
                                                   // "chat"       - simple chat
                                                   // "groupchat"  - conference
                                                   // "private"    - conference private
                                                   // "gprivate"   - message in conference to bot
    private String         AnsMsgType      = null; // Type of return message
    private String         Msg             = null; // Message
    private String         FuncName        = null; //
    private String         FuncId          = null; //
    private String         SplitMsg[]      = null; // <space> splitted string
    private String         PostMsg         = null; //
    private boolean        PostMsgFlag     = true; //
    private String         Ansver          = null; //
    private String         SessAnsver      = null; //
    private int            NextArg         = -1;   // next usable argument on the SplitMsg array
    private int            Executable      = -1;   // If message contain command var is true
    private int            Ansverable      = -1;   // 
    private int            Permittable     = -1;   //
    private int            SessionAttached = -1;   //
    private boolean        Parsed          = false;//
    private String         From            = null; // Sender's JID
    private String         ShortFrom       = null; // Sender's short name
    private user           User            = null; //
    private Packet         Packet          = null; // Message packet
    private MultiUserChat  Mchat           = null; // Main muc chat to creat new chats
    private XMPPConnection Connect         = null; // Main connectin to creat new chats

    public options(Date date, String MsgType, String Msg, String From, String ShortFrom, Packet packet, XMPPConnection connect, MultiUserChat mchat) {
        setDate(date);
        setMsgType(MsgType);
        setMsg(Msg);
        setFrom(From);
        setShortFrom(ShortFrom);
        setPacket(packet);
        setConnect(connect);
        setMchat(mchat);
    }
    
    public options(Date date, String MsgType, String Msg, String From, String ShortFrom, Packet packet, XMPPConnection connect) {
        setDate(date);
        setMsgType(MsgType);
        setMsg(Msg);
        setFrom(From);
        setShortFrom(ShortFrom);
        setPacket(packet);
        setConnect(connect);
    }
    
    public void setDate (Date date) {
        this.date = date;
    }

    public void setMsgType (String MsgType) {
        this.MsgType = MsgType;
    }

    public void setAnsMsgType (String AnsMsgType) {
        this.AnsMsgType = AnsMsgType;
    }

    public void setMsg (String Msg) {
        this.Msg = Msg;
    }

    public void setFuncName (String FuncName) {
        this.FuncName = FuncName;
    }
    
    public void setFuncId (String FuncId) {
        this.FuncId = FuncId;
    }
    
    public void setSplitMsg (String[] SplitMsg) {
        this.SplitMsg = SplitMsg;
    }

    public void setPostMsg (String PostMsg) {
        this.PostMsg = PostMsg;
    }

    public void setAnsver (String Ansver) {
        this.Ansver = Ansver;
    }
    
    public void setNextArg (int NextArg) {
        this.NextArg = NextArg;
    }

    public void setExecutable (boolean Executable) {
        int ans;
        if (Executable) {
            ans = 1;
        } else {
            ans = 0;
        }
        this.Executable = ans;
    }

    public void setAnsverable (boolean Ansverable) {
        int ans;
        if (Ansverable) {
            ans = 1;
        } else {
            ans = 0;
        }
        this.Ansverable = ans;
    }

    public void setPermittable (boolean Permittable) {
        int ans;
        if (Permittable) {
            ans = 1;
        } else {
            ans = 0;
        }
        this.Permittable = ans;
    }

    public void setSessionAttached (boolean SessionAttached) {
        int ans;
        if (SessionAttached) {
            ans = 1;
        } else {
            ans = 0;
        }
        this.SessionAttached = ans;
    }

    public void setFrom (String From) {
        this.From = From;
    }

    public void setShortFrom (String ShortFrom) {
        this.ShortFrom = ShortFrom;
    }

    public void setUser (user User) {
        this.User = User;
    }
    
    public void setPacket (Packet Packet) {
        this.Packet = Packet;
    }

    public void setMchat (MultiUserChat Mchat) {
        this.Mchat = Mchat;
    }

    public void setConnect (XMPPConnection Connect) {
        this.Connect = Connect;
    }
    
    public Date getDate () {
        return date;
    }
    
    public String getMsgType () {
        return MsgType;
    }
    
    public String getAnsMsgType () throws SQLException {
        String ans;
        if (AnsMsgType == null) {
            if (getAnsverable()) {
                ResultSet rs = MySQLDB.getStatement().executeQuery("SELECT val FROM sourcevals WHERE id = (SELECT sources.to FROM sources WHERE sources.from = (SELECT id FROM sourcevals WHERE val = '" + getMsgType() + "') AND cmd = (SELECT id FROM funcs WHERE func = '" + getFuncName() + "'));");
                rs.next();
                ans = rs.getString("val");
                setAnsMsgType(ans);
                db.killrs(rs); rs = null;
            } else {
                ans = null;
            }
        } else {
            ans = AnsMsgType;
        }
        return ans;
    }
    
    public String getMsg () {
        return Msg;
    }
    
    public String getFuncName () throws SQLException {
        String ans;
        if (!Parsed) {
            parser();
            ans = getFuncName();
        } else {
            ans = FuncName;
        }
        return ans;
    }
    
    public String getFuncId () throws SQLException {
        String ans;
        if (!Parsed) {
            parser();
            ans = getFuncId();
        } else {
            ans = FuncId;
        }
        return ans;
    }
    
    public String[] getSplitMsg () {
        String[] arr = null;
        if (SplitMsg == null) {
            arr = getMsg().split(" ");
            setSplitMsg(arr);
        } else {
            arr = SplitMsg;
        }
        return arr;
    }
    
    public String getPostMsg () throws SQLException {
        String postmsg;
        if (PostMsgFlag && getFuncName() != null) {
            String[] SpMsg = getSplitMsg();
            if (SpMsg.length != getNextArg() && getExecutable()) {
                postmsg = SpMsg[getNextArg()];
                for (int i = getNextArg() + 1; i < SpMsg.length; i++) {
                    postmsg = postmsg + " " + SpMsg[i];
                }
                setPostMsg(postmsg);
            }
            PostMsgFlag = false;
        }
        return PostMsg;
    }
    
    public String getAnsver () {
        return Ansver;
    }
    
    public int getNextArg () throws SQLException {
        int ans;
        if (getFuncName() != null) {
            ans = NextArg;
        } else {
            ans = -1;
        }
        return ans; 
    }
    
    public boolean getExecutable () throws SQLException {
        boolean ans;
        if (Executable == -1) {
            exeansquery();
            ans = getExecutable();
        } else {
            ans = Executable == 1;
        }
        return ans;
    }
    
    public boolean getAnsverable () throws SQLException {
        boolean ans;
        if (Ansverable == -1) {
            exeansquery();
            ans = getAnsverable();
        } else {
            ans = Ansverable == 1;
        }
        return ans;
    }
    
    public boolean getPermittable () throws SQLException {
        boolean ans;
        if (Permittable == -1) {
            if (getFuncName() != null) {
                ans = ((String) getUser().perms.get(getFuncName())).equals("1");
            } else {
                ans = false;
            }
            setPermittable(ans);
        } else {
            ans = Permittable == 1;
        }
        return ans; 
    }
    
    public boolean getSessionAttached () {
        if (Main.sessions.containsKey(getFrom())) {
            setSessionAttached(true);            
        } else {
            setSessionAttached(false);
        }
        return SessionAttached == 1;
    }
    
    public String getFrom () {
        return From;
    }
    
    public String getShortFrom () {
        return ShortFrom;
    }
    
    public user getUser () throws SQLException {
        if (User == null) {
            User = new user(ShortFrom);
        }
        return User;
    }

    public Packet getPacket () {
        return Packet;
    }
    
    public MultiUserChat getMchat () {
        MultiUserChat mchat;
        if (!getMsgType().equals("chat")) {
            mchat = Mchat;
        } else {
            mchat = null;
        }
        return mchat;
    }
    
    public XMPPConnection getConnect () {
        return Connect;
    }
    
    private void parser () throws SQLException {
        String[] SpMsg = getSplitMsg();
        String max_spaces = (String) Main.Connect.settings.get("max_spaces_in_cmd");
        ResultSet rs;
        boolean flag = true;
        int funcid = -1;
        int i = 1;
        String funcname = null;
        while (flag && i <= Integer.parseInt(max_spaces) && i <= SpMsg.length) {
            flag = false;
            String str = SpMsg[0];
            for (int j = 1; j<i; j++) {
                str = str + " " + SpMsg[j];
            }
            rs = MySQLDB.getStatement().executeQuery("SELECT id,func FROM funcs WHERE id = (SELECT func FROM funcsvals WHERE val = '" + utils.parseSymbols(str) + "');");
            funcname = null;
            while (rs.next()) {
                funcname = rs.getString("func");
                funcid = rs.getInt("id");
            }
            db.killrs(rs); rs = null;
            if (funcname == null && SpMsg.length > i) {
                flag = true;
                i++;
            }
        }
        setFuncName(funcname);
        setFuncId(Integer.toString(funcid));
        setNextArg(i);
        Parsed = true;
    }

    private void exeansquery () throws SQLException {
        ResultSet rs = MySQLDB.getStatement().executeQuery("SELECT exec,ans FROM sources WHERE cmd = (SELECT id FROM funcs WHERE func = '" + getFuncName() + "') AND sources.from = (SELECT id FROM sourcevals WHERE val = '" + getMsgType() +"');");
        if (rs.next()) {
            setExecutable(rs.getString("exec").equals("1"));
            setAnsverable(rs.getString("ans").equals("1"));
        } else {
            setExecutable(false);
            setAnsverable(false);
        }
        db.killrs(rs); rs = null;
    }
}